package com.jnxx.okr.service.impl;

import com.jnxx.basic.service.impl.BaseServiceImpl;
import com.jnxx.okr.domain.Alignment;
import com.jnxx.okr.domain.Okr;
import com.jnxx.okr.mapper.AlignmentMapper;
import com.jnxx.okr.mapper.OkrMapper;
import com.jnxx.okr.service.IAlignmentService;
import com.jnxx.task.mapper.OkrStaffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 业务实现类：
 */
@Service
public class AlignmentServiceImpl extends BaseServiceImpl<Alignment> implements IAlignmentService {

    @Autowired
    private OkrStaffMapper staffMapper;

    @Autowired
    private AlignmentMapper alignmentMapper;

    @Autowired
    private OkrMapper okrMapper;

    @Override
    public void addAlignmentForOkr(List<Okr> okrList, Long sourceId) {
        // 查询原来有无
        List<Alignment> alignmentList = alignmentMapper.findBySourceId(sourceId);
        // 原来的okr
        Okr sourceOkr = okrMapper.findOne(sourceId);

        if (alignmentList.size() > 0){
            // 查询原来对齐，大于零，说明原来有，先删除原来的
            for (Alignment alignment : alignmentList) {
                Okr okr = okrMapper.findOne(alignment.getTargetId());
                super.del(alignment.getId());
                updateKrProgress(okr, alignment.getTargetId());
            }
//            alignmentMapper.delBySourceId(sourceId);
        }
        // 如果有
        if (okrList != null && okrList.size() > 0 ){
            // 使用Java Stream过滤掉空对象
            okrList = okrList.stream()
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            for (Okr okr : okrList) {
                Alignment alignment = new Alignment();
                if (okr != null){
                    alignment.setSourceName(sourceOkr.getName());
                    alignment.setSourceId(sourceId);
                    alignment.setType(sourceOkr.getType());
                    alignment.setTargetId(okr.getId());
                    alignment.setTargetName(okr.getName());
                    alignment.setParentId(okr.getParentId());
                    alignment.setCycleId(okr.getCycleId());

                    String sourceName = staffMapper.findNameById(sourceOkr.getLeader());
                    String targetName = staffMapper.findNameById(okr.getLeader());
                    alignment.setSourceLeaderName(sourceName);
                    alignment.setTargetLeaderName(targetName);

                    if (okr.getParentId() != null){
                        String okrName= okrMapper.findNameById(okr.getParentId());
                        alignment.setOkrName(okrName);
                    }
                    super.add(alignment);
                    updateKrProgress(okr, okr.getId());
                }
            }
        }
    }

    @Override
    public void del(Long id) {

        Alignment alignment = super.findOne(id);

        Okr okr = okrMapper.findOne(alignment.getTargetId());
        super.del(id);
        updateKrProgress(okr, alignment.getTargetId());
    }

    // 更新添加或者删除对齐，更新kr进度
   public void updateKrProgress(Okr okr, Long id){
       // 在查
       Integer[] okrIds = alignmentMapper.findIdsByTargetIdOrParentId(id);

       // 查到下面所有okr
       List<Okr> byIds = new ArrayList<>();
       if (okrIds.length > 0){
            byIds = okrMapper.findByIds(okrIds);
       }
       if(byIds.size() > 0){
           double averageProgress = byIds.stream()
                   .mapToDouble(Okr::getProgress)
                   .average()
                   .orElse(0.0);
           okr.setProgress((int) averageProgress);
           okr.setAlignmentProgress(1);
       } else {
           okr.setProgress(0);
           okr.setAlignmentProgress(0);
       }
       okrMapper.update(okr);
       // 跟新主okr进度
       if (okr.getParentId() != null){
           Okr parent = okrMapper.findParent(okr.getParentId());
           List<Okr> subOkrList = okrMapper.findByParentId(parent.getId());
           if (subOkrList.size() > 0 ){
               double averageProgress = subOkrList.stream()
                       .mapToDouble(Okr::getProgress)
                       .average()
                       .orElse(0.0);
               parent.setProgress((int) averageProgress);
           }
           okrMapper.update(parent);
       }
   }

    /**
     * 当前okr更新完成进度之后，在往上级找，更新上级任务
     * @param parent
     */
   public void updateHigherTask(Okr parent){
       List<Alignment> bySourceId = alignmentMapper.findBySourceId(parent.getId());
       if (bySourceId.size() > 0){
           for (Alignment alignment : bySourceId) {
               List<Alignment> byTargetId = alignmentMapper.findByTargetId(alignment.getTargetId());


               Okr okr = okrMapper.findOne(alignment.getTargetId());
               if (okr != null){
                   List<Okr> subOkrList = okrMapper.findByParentId(okr.getId());
                   double averageProgress = subOkrList.stream()
                           .mapToDouble(Okr::getProgress)
                           .average()
                           .orElse(0.0);
                   okr.setProgress((int)averageProgress);
                   okrMapper.update(okr);
               }
           }
       }


   }

}
